package com.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.configuration.Configuration;
import com.configuration.Owner;
import com.user.JSONParser;

public class UserFollow {
	//URL for fetching data
    private static String followUserByIdURL = "http://128.199.214.245/android/UserFollow/follow_user_by_id.php";
    private static String unfollowUserByIdURL = "http://128.199.214.245/android/UserFollow/unfollow_user_by_id.php";
    private static String isFollowingByUserIdURL = "http://128.199.214.245/android/UserFollow/is_following_by_user_id.php";
    
    
    //Keys for the Json field
    /*
     * KEY_STATUS shows the status of the request
     * 0 : Fail to get user
     * 1 : Successfully in getting user
     */
    private String KEY_STATUS = "STATUS";
    private String KEY_FOLLOWING ="FOLLOWING";
    private String KEY_ID = "ID";
    private String KEY_NAME = "NAME";
    private String KEY_DESCRIPTION = "DESCRIPTION";
    private String KEY_PROFILE_PICTURE = "PROFILE_PICTURE";
    
    private String FAIL = "0";
    private String SUCCESS = "1";
    
    private String FOLLOWING = "1";
    
    //For making httpRequest
    private JSONParser jsonParser;
    private JSONObject result;
    List<NameValuePair> params;
    
    
    //Constructor
    public UserFollow(){
        jsonParser = new JSONParser();
    }
    
    public int followUserById(int user_follow){
		//Get Json object
		// Building Parameters
        params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("user",Integer.toString(Owner.id)));
        params.add(new BasicNameValuePair("user_follow",Integer.toString(user_follow)));
        
        
        //Start a new thread and use POST method to send telephone and passcode to authURL
        //Start a new thread to attempt Login (requirement when connecting online)
		Thread attemptThread = new Thread(new Runnable(){  
			public void run(){  
				result = jsonParser.makeHttpRequest(followUserByIdURL,"POST",params);
			}
		});  
		attemptThread.start();
		
		while(attemptThread.isAlive()){
			//Wait for server response
		}
		
		//Parse the json object
		try {
			if(result.getString(KEY_STATUS) != null){
				//Able to get response from server
				if(result.getString(KEY_STATUS).equalsIgnoreCase(SUCCESS)){
					//Successful in finding the user
					if(Configuration.showLog){
						Log.d("UserFollow","followUserById - follow ok");
					}
					
					return Integer.parseInt(SUCCESS);
					
				}else{
					//Fail to find such user
					if(Configuration.showLog){
						Log.d("UserFollow","followUserById - fail");
					}
				}
			}else{
				//unable to get response from server
				if(Configuration.showLog){
					Log.d("UserFollow","followUserById - Unable to get response from server");
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			if(Configuration.showLog){
				Log.d("UserFollow","followUserById - Exception");
			}
		}

		return Integer.parseInt(FAIL);
	}
    
    public int unfollowUserById(int user_follow){
		//Get Json object
		// Building Parameters
        params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("user",Integer.toString(Owner.id)));
        params.add(new BasicNameValuePair("user_follow",Integer.toString(user_follow)));
        
        
        //Start a new thread and use POST method to send telephone and passcode to authURL
        //Start a new thread to attempt Login (requirement when connecting online)
		Thread attemptThread = new Thread(new Runnable(){  
			public void run(){  
				result = jsonParser.makeHttpRequest(unfollowUserByIdURL,"POST",params);
			}
		});  
		attemptThread.start();
		
		while(attemptThread.isAlive()){
			//Wait for server response
		}
		
		//Parse the json object
		try {
			if(result.getString(KEY_STATUS) != null){
				//Able to get response from server
				if(result.getString(KEY_STATUS).equalsIgnoreCase(SUCCESS)){
					//Successful in finding the user
					if(Configuration.showLog){
						Log.d("UserFollow","unfollowUserById - unfollow ok");
					}
					
					return Integer.parseInt(SUCCESS);
					
				}else{
					//Fail to find such user
					if(Configuration.showLog){
						Log.d("UserFollow","unfollowUserById - fail");
					}
				}
			}else{
				//unable to get response from server
				if(Configuration.showLog){
					Log.d("UserFollow","unfollowUserById - Unable to get response from server");
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			if(Configuration.showLog){
				Log.d("UserFollow","unfollowUserById - Exception");
			}
		}

		return Integer.parseInt(FAIL);
	}
    
    public boolean isFollowingByUserId(int user_follow){
		//Get Json object
		// Building Parameters
        params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("user",Integer.toString(Owner.id)));
        params.add(new BasicNameValuePair("user_follow",Integer.toString(user_follow)));
        
        
        //Start a new thread and use POST method to send telephone and passcode to authURL
        //Start a new thread to attempt Login (requirement when connecting online)
		Thread attemptThread = new Thread(new Runnable(){  
			public void run(){  
				result = jsonParser.makeHttpRequest(isFollowingByUserIdURL,"POST",params);
			}
		});  
		attemptThread.start();
		
		while(attemptThread.isAlive()){
			//Wait for server response
		}
		
		//Parse the json object
		try {
			if(result.getString(KEY_STATUS) != null){
				//Able to get response from server
				if(result.getString(KEY_FOLLOWING).equalsIgnoreCase(FOLLOWING)){
					//Owner is following that user
					return true;
				}else{
					//Owner is not following that user
					return false;
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			if(Configuration.showLog){
				Log.d("UserFollow","checkIsFollowingByUserId - Exception");
			}
		}

		return false;
	}
}
